/*
 Copyright (c) 2013 MeherTJ G. All rights reserved.
 License: LGPL for personnal study or free software.
 */

#pragma once
#include "zen_al_platform.h"
#include "zen_exception.h"

namespace Zen {
	enum class eALTraceType
	{
		Mono8 = AL_FORMAT_MONO8,
		Mono16 = AL_FORMAT_MONO16,
		Stereo8 = AL_FORMAT_STEREO8,
		Stereo16 = AL_FORMAT_STEREO16,
	};
	class ALBuffer
	{
	protected:
		ALuint mBuffer;

		ALBuffer(ALBuffer const&) = delete;

		void operator = (ALBuffer const&) = delete;

	public:
		inline ALBuffer();

		inline ~ALBuffer();

		inline void Generate();

		inline void Release();

		inline void BindData(eALTraceType fmt, void* value, size_t size, size_t freq) const;

		inline ALuint GetALBufferID() const;
	};
}


namespace Zen {
	inline ALBuffer::ALBuffer()
	{
		mBuffer = 0;
	}
	inline ALBuffer::~ALBuffer()
	{
		Release();
	}
	inline void ALBuffer::Generate()
	{
		if (mBuffer != 0) return;
		alGenBuffers(1, &mBuffer);
		__zen_must_else(mBuffer != 0, "failed to Generate openal buffer");
	}
	inline void ALBuffer::Release()
	{
		if (mBuffer != 0)
		{
			alDeleteBuffers(1, &mBuffer);
			mBuffer = 0;
		}
	}
	inline void ALBuffer::BindData(eALTraceType fmt, void* value, size_t size, size_t freq) const
	{
		alBufferData(mBuffer, (ALenum)fmt, value, (int)size, (int)freq);
	}
	inline ALuint ALBuffer::GetALBufferID() const
	{
		return mBuffer;
	}
}
